package com.huang.Service.ServiceImpl;

import com.huang.Dao.OrderDao;
import com.huang.Pojo.Order;
import com.huang.Pojo.Product;
import com.huang.Service.FeignClientService;
import com.huang.Service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Service
public class OrderServiceImpl04 {

    @Resource
    private OrderDao orderDao;

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Resource
    private FeignClientService feignClientService;


    @GlobalTransactional
    public Order createOrder(Integer pid) {
        //        RestTemplete——调用商品微服务
//        Product product = restTemplate.getForObject(Product_Url+"/product/"+pid, Product.class);
//        FeignClient——调用商品微服务
        Product product = feignClientService.product(pid);
//        下单（创建订单）
        Order order = new Order();
        order.setUid(2);
        order.setUsername("测试用户02");
        order.setPid(pid);
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(2);

        orderDao.save(order);

        feignClientService.reduceInventory(pid,order.getNumber());

        rocketMQTemplate.convertAndSend("topic-order", order);
        
        return order;
    }

}
